var hIndex = function (citations) {
  let len = citations.length;
  let sum = 0;
  let h = -1;

  for (let i = 0; i < len; i++) {
    sum += citations[i];
  }
  let avg = sum / len;

  for (let i = 0; i < len; i++) {
    if (citations[i] <= avg && citations[i] >= h) {
      h = citations[i];
    }
  }

  return h;
};

// 解法二，使用排序的方式解决
var hIndex = function (citations) {
  citations.sort((a, b) => a - b);
  let h = 0, i = citations.length - 1;
  while (i >= 0 && citations[i] > h) {
    h++;
    i--;
  }
  return h;
};

let citations1 = [3, 0, 6, 1, 5];
let citations2 = [1, 3, 1];

console.log(hIndex(citations2));
